﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FloatWindow
{
    public partial class Form1 : Form
    {
        private Point ptMouseCurrrnetPos, ptMouseNewPos, ptFormPos, ptFormNewPos;
        private bool blnMouseDown = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FormTopMost_Load(sender, e);
        }

        /// <summary>
        /// 悬浮窗口的Load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormTopMost_Load(object sender, EventArgs e)
        {
            this.Show();
            this.Top = Screen.PrimaryScreen.Bounds.Height - 300;
            this.Left = Screen.PrimaryScreen.Bounds.Width - 200;
            this.Width = 120;
            this.Height = 80;
            // 无标题
            this.FormBorderStyle = FormBorderStyle.None;
            //this.BackColor = Color.FromArgb(32, 8, 8, 8);


            this.MouseDown += this.FormTopMost_MouseDown;
            this.MouseUp += this.FormTopMost_MouseUp;
            this.MouseMove += this.FormTopMost_MouseMove;

            // 任务栏不显示
            this.ShowInTaskbar = false;
            // 点击穿透
            // this.BackColor = SystemColors.Control;
            // this.TransparencyKey = System.Drawing.SystemColors.Control;
            // 最上层显示
            this.TopMost = true;
           
        }




        /// <summary>
        /// 移动鼠标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormTopMost_MouseMove(object sender, MouseEventArgs e)
        {
            if (blnMouseDown)
            {
                ptMouseNewPos = Control.MousePosition;
                ptFormNewPos.X = ptMouseNewPos.X - ptMouseCurrrnetPos.X + ptFormPos.X;
                ptFormNewPos.Y = ptMouseNewPos.Y - ptMouseCurrrnetPos.Y + ptFormPos.Y;
                Location = ptFormNewPos;
                ptFormPos = ptFormNewPos;
                ptMouseCurrrnetPos = ptMouseNewPos;
            }
        }
        /// <summary>
        /// 鼠标点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormTopMost_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                blnMouseDown = true;
                ptMouseCurrrnetPos = Control.MousePosition;
                ptFormPos = Location;
            }
        }
        /// <summary>
        /// 鼠标起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormTopMost_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                blnMouseDown = false;
        }
    }
}
